<template>
  <!-- This section should be hidden by default and shown when there are todos -->
  <section class="main">
    <input
      id="toggle-all"
      class="toggle-all"
      type="checkbox"
      :checked="isAllChecked"
      @change="changeFn"
    />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <!-- These are here just to show the structure of the list items -->
      <!-- List items should get the class `editing` when editing and `completed` when marked as completed -->
      <li
        :class="{ completed: obj.isDone }"
        v-for="(obj, index) in arr"
        :key="obj.id"
      >
        <div class="view">
          <input class="toggle" type="checkbox" v-model="obj.isDone" />
          <label>{{ obj.name }}</label>
          <button class="destroy" @click="del(index)"></button>
        </div>
      </li>
    </ul>
  </section>
</template>

<script>
export default {
  props: ["arr"],
  methods: {
    del(index) {
      if (confirm("确定要删除么?")) {
        this.$emit("del-todo", index);
      }
    },
    changeFn(e) {
      this.arr.forEach((item) =>  item.isDone = e.target.checked);
    },
  },
  computed: {
    isAllChecked() {
      return this.arr.every((item) => item.isDone == true);
    },
  },
};
</script>

<style>
</style>